package com.lz.ionicbase.ui.activity;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lz.ionicbase.R;
import com.lz.ionicbase.engine.SetHelper;
import com.lz.ionicbase.ui.activity.action.IFeedBackActivity;
import com.lz.ionicbase.utils.BaseUtil;
import com.lz.ionicbase.utils.Constant;
import com.lz.ionicbase.utils.SpUtils;
import com.lz.ionicbase.widget.Notice;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;


/**
 *
 */
public class FeedBackActivity extends BaseActivity implements IFeedBackActivity {

    @InjectView(R.id.customTitle_tv) TextView customTitle_tv;
    @InjectView(R.id.right_bt) Button right_bt;
    @InjectView(R.id.feedBack_et) EditText feedBack_et;

    private Context context;

    private SetHelper setHelper;

    @Override
    protected int getActivityLayout() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void initView() {
        context = this;
        ButterKnife.inject(this);

        customTitle_tv.setText(R.string.help_feedback);
        right_bt.setVisibility(View.VISIBLE);

        setHelper = new SetHelper(FeedBackActivity.this);
    }

    @Override
    protected void initListener() {

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        setHelper.onDestroy();
    }


    @OnClick({R.id.my_back,  R.id.right_bt})
    public void onClick(View view) {
        switch (view.getId()){
                 case R.id.my_back:
                     finish();
                      break;
                 case R.id.right_bt:
                     FeedBack();
                      break;
                 default:
                      break;
                    }
    }

    private void FeedBack() {
        String message = feedBack_et.getText().toString().trim();
        if(TextUtils.isEmpty(message)){
            return;
        }

        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = telephonyManager.getDeviceId();
        String token = SpUtils.getString(context, Constant.TOKEN, "");
        Map<String,String> param = new HashMap();
        param.put("uid",deviceId);
        param.put("token",token);
        param.put("msessage", BaseUtil.getBase64(message));

        String baseUrl = SpUtils.getString(context, SpUtils.KEY_BASE_URL,"");
        String url = baseUrl+ Constant.FEEDBACK;
        setHelper.feedBack(url,param);
    }

    @Override
    public void onFeedBackSuccess() {
        Toast.makeText(context,"反馈成功!",Toast.LENGTH_LONG).show();
    }

    @Override
    public void onFeedBackError() {
        Toast.makeText(context,"反馈失败!",Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDataException() {
        Notice.dataException(context);
    }

}
